//
//  DisButtonImageTable.m
//  ImageReplacer
//
//  Created by Tyler Powell on 5/7/12.
//  Copyright (c) 2012 Georgia Institute of Technology. All rights reserved.
//

#import "DisButtonImageTable.h"

@implementation DisButtonImageTable

@synthesize imageView;
@synthesize writer;

- (int)numberOfRowsInTableView:(NSTableView*) tableView {
    //int count = countLines(@"disabledButtons.txt");
	//return count>0 ? count-1 : 0;
    return [[[tableView writer] disbuttons] count];
}
- (id)tableView:(NSTableView*) tableView
objectValueForTableColumn:NSTableColumn
            row:(int)row{
    //NSMutableArray* marray = [NSMutableArray arrayWithArray:getLines(@"images.txt")];
    //[marray removeLastObject];
    //NSLog(@"Row: %d",row);
    /*
    NSArray* originalLines = getLines(@"disabledButtons.txt");
    NSMutableArray* mutableLines = [NSMutableArray arrayWithArray:originalLines];
    for (int i = 0; i<[originalLines count]; i++) {
        NSString* imgpath = [originalLines objectAtIndex:i];
        NSArray* components = [imgpath componentsSeparatedByString:@"/"];
        [mutableLines replaceObjectAtIndex:i withObject:[components lastObject]];
    }
	return[mutableLines objectAtIndex:row];
     */
    return[[[[tableView writer] disbuttons] objectAtIndex:row] lastPathComponent];
}
- (void) tableView:(NSTableView*)tableView
    setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSString* current = [[[tableView writer] disbuttons] objectAtIndex:row];
    NSString* currentFileType = [current pathExtension];
    NSString* newFileType = [object pathExtension];
    if(![currentFileType isEqualToString:newFileType])
        return;
    //NSString* file =[[current lastPathComponent]stringByDeletingPathExtension];
    NSString* newPath = [current stringByDeletingLastPathComponent];
    newPath = [newPath stringByAppendingString:@"/"];
    newPath = [newPath stringByAppendingString:object];
    //NSURL* currentPath = [NSURL URLWithString:current];
    [[NSFileManager defaultManager] movePath:current toPath:newPath handler:nil];
    //writeToFileAtIndex(@"disabledButtons.txt", newPath, row);
    [[tableView writer] replaceDisabledButtonAtIndex:row withImage:newPath];
}
- (void)removeItem:(id)sender{
	id selection = [self selectedRowIndexes];
	int index = [selection firstIndex];
	//removeItemFromFile(@"disabledButtons.txt", index);
    [writer removeDisabledButton:index];
	[self reloadData];
    [imageView setSelectedImage:self];
}
- (void)refreshTable:(id)sender{
	[self reloadData];
	//[self display];
}
- (void)saveSelection:(id)sender{
	id selection = [self selectedRowIndexes];
	if([selection count]==0)
		return;
	int index = [selection firstIndex];
	//NSString* indexString = getItemFromFileAtIndex(@"disabledButtons.txt", index);
    NSString* indexString = [[writer disbuttons] objectAtIndex:index];
    //NSLog(indexString);
	//writeToFileAtIndex(@"options.txt", indexString,4);
    [writer replaceOptionAtIndex:4 withOption:indexString];
}

@end
